package com.lanmon.springmvc.converter;

import java.io.IOException;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.lanmon.util.DateUtil;

/**
 * 用于将实体序列化到前台的时候转化Date格式。实体getter上设置@JsonSerialize(using=CustomDateConverter.
 * class)
 * 弊端：不同Date格式需要提供多个实现类，暂时为找到可以传入Date格式参数的方法
 * 
 * @author Administrator
 * 
 */
public class CustomDateConverter extends JsonSerializer<Date> {
	
	@Override
	public void serialize(Date arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException, JsonProcessingException {
		arg1.writeString(DateUtil.formatDate(arg0, "yyyy-MM-dd"));
	}
	
}